package com.example.androidcoursedesign.main.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.androidcoursedesign.R;
import com.example.androidcoursedesign.main.bean.SolarTermResponse;

import java.util.List;

public class SolarTermAdapter extends RecyclerView.Adapter<SolarTermAdapter.SolarTerItemViewHolder> {
    private  Context mContent;

    private  LayoutInflater inflater;

    private List<SolarTermResponse.ListBean> data;

    public SolarTermAdapter(Context context,List<SolarTermResponse.ListBean> data){
        this.mContent=context;
        inflater= LayoutInflater.from(mContent);
        this.data=data;
    }

    public void  setData(List<SolarTermResponse.ListBean> data){
        this.data=data;
    }


    @NonNull
    @Override
    public SolarTerItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view = inflater.inflate(R.layout.item_layout, parent, false);
        SolarTerItemViewHolder solarTerItemViewHolder = new SolarTerItemViewHolder(view);
        return solarTerItemViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull SolarTerItemViewHolder holder, int position) {
        //标题
        SolarTermResponse.ListBean itemBean = data.get(position);
        holder.title_tv.setText(itemBean.getTitle());
        //内容
        holder.content_tv.setText(itemBean.getContent());
        //加载图片
  // this：上下文   url：要加载的图片链接   imageView: 要显示的图片控件
//            Glide.with(this).load(url).into(imageView);
        Glide.with(mContent).load(itemBean.getUrl()).into(holder.item_imageview);


    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    class SolarTerItemViewHolder extends  RecyclerView.ViewHolder{
         private ImageView item_imageview;
         private TextView title_tv;
         private TextView content_tv;
        public SolarTerItemViewHolder(@NonNull View itemView) {
            super(itemView);
            item_imageview = itemView.findViewById(R.id.item_imageview);
            title_tv= itemView.findViewById(R.id.title_tv);
            content_tv= itemView.findViewById(R.id.content_tv);
        }
    }

}
